package com.woniuxy.yoga.user.service.mq;

import com.woniuxy.yoga.user.dao.mapper.UserLoginLogMapper;
import com.woniuxy.yoga.user.dao.model.UserLoginLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class CreateLoginLogConsumer {

    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    @RabbitListener(queuesToDeclare = {@Queue(name = "user.loginLog")})
    @RabbitHandler
    public void loginLog(Map<String,Object> message){
        log.info("创建用户登录日志:{}",message);
        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setUserId(((Integer) message.get("userId")));
        userLoginLog.setUserName(message.get("userName").toString());
        userLoginLog.setUserPhone(message.get("userPhone").toString());
        userLoginLog.setLoginTime(message.get("loginTime").toString());
        userLoginLog.setLoginAddress(message.get("ip").toString());
        userLoginLogMapper.insert(userLoginLog);
    }
}
